package com.qf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sys.po.Log;
import com.qf.sys.po.Module;
import com.qf.sys.service.LogService;
import com.qf.sys.util.LayUIOperate;
import com.qf.sys.util.Msg;
import com.qf.sys.util.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @创建人 sunsir Email:sunsir163@163.com
 * @创建时间 2022/5/8
 * @描述
 */
@Controller
@RequestMapping("log")
public class LogController {
    @Resource
    LogService logService;

    @RequestMapping("addLog")
    @ResponseBody
    public LayUIOperate addLog(@RequestBody Log log){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("addLog -->"+log);
        boolean f= logService.addLog(log);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("addLog添加成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("addLog添加失败");
        }
        return operate;
    }

    @RequestMapping("getLogList")
    @ResponseBody
    public Msg getLogList(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String _empNo = request.getParameter("empNo");
        int empNo = 0;
        if (_empNo != null && !"".equals(_empNo)) {
            empNo = Integer.parseInt(_empNo);
        }
        String eName = request.getParameter("eName");

        String mName = request.getParameter("mName");
        String log = request.getParameter("log");

        String _startTime = request.getParameter("startTime");
        String _endTime = request.getParameter("endTime");
        Date startTime = null;
        Date endTime = null;
        if (_startTime!=null){
            startTime =  PageUtils.stringToDate2(_startTime);
        }
        if (_endTime!=null){
            endTime = PageUtils.stringToDate2(_endTime);
        }
        Map params = new HashMap();
        params.put("mName", mName);
        params.put("empNo", empNo);
        params.put("eName", eName);
        params.put("log", log);
        if(startTime!=null){
            params.put("startTime",startTime);
        }
        if (endTime!=null){
            params.put("endTime",endTime);
        }

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Log> data = logService.getLogList(params);
        Msg msg = new Msg();
        msg.setCode(0);
        msg.setMsg("处理成功");
        msg.setCount(data.getTotal());//得到总条数
        msg.setData(data.getList());//得到查出的List数据
        System.out.println(msg);
        return msg;
    }

}
